<?php
namespace app\admin\controller\v1;
use app\admin\Adminbase;

class GoodsCate extends Adminbase 
{
	
	/**
	* 获取商品分类
	* /admin/v1/goods_cate/xmlGoodsCate
	* 需要登录 
	* @is_need_login true
	* @method post
	*/
	public function xmlGoodsCate($shop_id=''){
		$request =  request();
		$shop_id = $shop_id ?: $request->userInfo['shop_id'];
		try {
			$model = model('GoodsCate');
			$model = $model->where("is_del",0);
			$model = $model->where("shop_id",$shop_id ?: 0);
			$model = $model->field("id,pid,name as label,id as value");
			$model = $model->order("sort_no");
			$cates = $model->select();
			$data = [];
			foreach($cates as $key => $value){
				if($value['pid']===0 && $value->childs()->count()<1){
				
				}else{
					$data[] = $value;
				}
			}
			return $request->isAjax() ? $this->ReSucess($data) : $data;	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 获取商品父级分类
	* /admin/v1/goods_cate/xmlGoodsParentCate
	* 需要登录 
	* @is_need_login true
	* @method post
	*/
	public function xmlGoodsParentCate($shop_id=''){
		$request =  request();
		$shop_id = $shop_id ?: $request->userInfo['shop_id'];
		try {
			$model = model('GoodsCate');
			$model = $model->where("is_del",0);
			$model = $model->where("pid",0);
			$model = $model->where("shop_id",$shop_id ?: 0);
			$model = $model->field("id,pid,name as label,id as value");
			$model = $model->order("sort_no");
			$cates = $model->select()->toArray();
			array_unshift($cates,["label"=>"无","value"=>"0"]);    
			return $request->isAjax() ? $this->ReSucess($cates) : $cates;	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}

    /**
     * 获取商品二级分类
     * /admin/v1/goods_cate/xmlGoodsPersonCate
     * 需要登录
     * @is_need_login true
     * @method post
     */
    public function xmlGoodsPersonCate($shop_id=''){
        $request =  request();
        $shop_id = $shop_id ?: $request->userInfo['shop_id'];
        try {
            $model = model('GoodsCate');
            $model = $model->where("is_del",0);
            $model = $model->where("pid","<>",0);
            $model = $model->where("shop_id",$shop_id ?: 0);
            $model = $model->field("id as value,name as label");
            $model = $model->order("sort_no");
            $cates = $model->select()->toArray();

            return $request->isAjax() ? $this->ReSucess($cates) : $cates;
        } catch (\Exception $e) {
            return $this->ReError($e->getMessage());
        }
    }
    
    
     /**
     * 获取商品二级分类
     * /admin/v1/goods_cate/getGoodsCates
     * 需要登录
     * @is_need_login true
     * @method post
     */
    public function getGoodsCates($shop_id=''){
        $request =  request();
        $shop_id = $shop_id ?: $request->userInfo['shop_id'];
        try {
        	$goods =  model('GoodsCate')->getList(request()->shopId,'0');
        	$service =  model('GoodsCate')->getList(request()->shopId,'2');
            return $this->ReSucess(['service'=>$service,'goods'=>$goods]);
        } catch (\Exception $e) {
            return $this->ReError($e->getMessage());
        }
    }

	//<!--  = 排序调整 =  -->
	//<!--  = /admin/v1/goods_cate/sortMove =  -->
	public function sortMove($moveId,$coverId){
		if( empty($moveId) )
			throw new \think\Exception('moveId参数不能为空');
		if( empty($coverId) )
			throw new \think\Exception('coverId参数不能为空');
		
		if($moveId==-1 || $coverId==-1){
			return $this->ReSucess("","操作成功");
		} 
		
		$result = model('GoodsCate')->updateSort($moveId,$coverId);
		if($result){
			return $this->ReSucess("","操作成功");
		}else{
			return $this->ReError("未知错误！");
		}
		
	}
	
}



